﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Jy123.BLL.Public;

namespace Jy123.BLL.User
{
    public class Mood
    {
        ///// <summary>
        ///// 用户的心情列表
        ///// </summary>
        ///// <param name="userID">用户ID</param>
        ///// <param name="count">需要获取的记录数量</param>
        ///// <returns>数据集</returns>
        ////public DataSet MoodList_My(int userID, int count)
        ////{
        ////    StringBuilder strSql = new StringBuilder();
        ////    strSql.Append("select TOP(@Count) a.ID,a.FUser,a.MoodContent,a.PostTime,b.UserName,b.Portrait,a.MType ");
        ////    strSql.Append("from User_Mood a inner join User_Main b on a.FUser=b.ID where (a.IsLock=0 and (a.FUser = @UserID)) ");
        ////    strSql.Append("order by a.ID desc");
        ////    SqlParameter[] parameters = { 
        ////                                new SqlParameter("@UserID", SqlDbType.Int, 4),
        ////                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        ////    parameters[0].Value = userID;
        ////    parameters[1].Value = count;
        ////    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        ////}

        /// <summary>
        /// 用户的心情列表
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">每一页的记录容量</param>
        /// <param name="counts">需要获取的记录数量</param>
        /// <returns>数据集</returns>
        public DataSet MoodList_My(int userID,string fields,int cpage, int psize, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = "(IsLock=0 and (FUser = '"+userID+"'))",
                CurrentPage = cpage,
                Fields = fields,
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = "ID DESC",
                Tables = "V_User_MoodList"
            }, out counts);
        }
        ///// <summary>
        ///// 用户的好友的心情列表
        ///// </summary>
        ///// <param name="userID">用户ID</param>
        ///// <param name="count">需要获取的记录数量</param>
        ///// <returns>数据集</returns>
        ////public DataSet MoodList_Friend(int userID, int count)
        ////{
        ////    StringBuilder strSql = new StringBuilder();
        ////    strSql.Append("select TOP(@Count) a.ID,a.FUser,a.MoodContent,a.PostTime,b.UserName,b.Portrait,a.MType ");
        ////    strSql.Append("from User_Mood a inner join User_Main b on a.FUser=b.ID ");
        ////    strSql.Append("where (a.IsLock=0 and (a.FUser in (select FFriend from User_Friend where FUser=@UserID) and a.FUser!=@UserID))");
        ////    strSql.Append("order by a.ID desc");
        ////    SqlParameter[] parameters = { 
        ////                                new SqlParameter("@UserID", SqlDbType.Int, 4),
        ////                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        ////    parameters[0].Value = userID;
        ////    parameters[1].Value = count;
        ////    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        ////}

        /// <summary>
        /// 用户的好友的心情列表
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">每一页的记录容量</param>
        /// <param name="counts">需要获取的记录数量</param>
        /// <returns>数据集</returns>
        public DataSet MoodList_Friend(int userID, int cpage, int psize, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = "(IsLock=0 and (FUser in (select FFriend from User_Friend where FUser='" + userID + "') and FUser!='" + userID + "'))",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                //Sort = "PostTime DESC",
                Tables = "V_User_MoodList"
            }, out counts);
        }
        ///// <summary>
        ///// 用户及其好友的心情列表
        ///// </summary>
        ///// <param name="userID">用户ID</param>
        ///// <param name="count">需要获取的记录数量</param>
        ///// <returns>数据集</returns>
        ////public DataSet MoodList_MeAndFriend(int userID, int count)
        ////{
        ////    StringBuilder strSql = new StringBuilder();
        ////    strSql.Append("select TOP(@Count) a.ID,a.FUser,a.MoodContent,a.PostTime,b.UserName,b.Portrait,a.MType ");
        ////    strSql.Append("from User_Mood a inner join User_Main b on a.FUser=b.ID ");
        ////    strSql.Append("where (a.IsLock=0 and (a.FUser in (select FFriend from User_Friend where FUser=@UserID) or a.FUser=@UserID))");
        ////    strSql.Append("order by a.ID desc");
        ////    SqlParameter[] parameters = { 
        ////                                new SqlParameter("@UserID", SqlDbType.Int, 4),
        ////                                new SqlParameter("@Count", SqlDbType.Int, 4)};
        ////    parameters[0].Value = userID;
        ////    parameters[1].Value = count;
        ////    return DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
        ////}

        /// <summary>
        /// 用户及其好友的心情列表
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">每一页的记录容量</param>
        /// <param name="counts">需要获取的记录数量</param>
        /// <returns>数据集</returns>
        public DataSet MoodList_MeAndFriend(int userID, int cpage, int psize, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = "(IsLock=0 and (FUser in (select FFriend from User_Friend where FUser='" + userID + "') or FUser='" + userID + "'))",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                //Sort = "PostTime DESC",
                Tables = "V_User_MoodList"
            }, out counts);
        }

        /// <summary>
        /// 所有人的心情列表，按最新排序
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页记录大小</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet MoodList_NewAll(int userID, int cpage, int psize, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = "(IsLock=0 and (FUser<>'" + userID + "') )",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = "PostTime DESC",
                Tables = "V_User_MoodList"
            }, out counts);
        }

        /// <summary>
        /// 所有的心情列表(后台)
        /// </summary>        
        /// <param name="cpage">当前页</param>
        /// <param name="psize">每一页的记录容量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">需要获取的记录数量</param>
        /// <returns>数据集</returns>
        public DataSet MoodList(int cpage, int psize, string strWhere, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                //Sort = "PostTime DESC",
                Tables = "V_User_MoodList"
            }, out counts);
        }

        /// <summary>
        /// 根据心情ID查询心情评论列表
        /// </summary>
        /// <param name="moodID">心情ID</param>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页大小</param>
        /// <param name="counts">页记录数</param>
        /// <returns>数据集</returns>
        public DataSet MoodCommentListByMoodID(int moodID, int cpage, int psize, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = "Fmood='"+moodID+"'",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                //Sort = "PostTime DESC",
                Tables = "V_User_MoodComment"
            }, out counts);
        }

        /// <summary>
        /// 取得用户的最新一条心情
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        public string GetUserMoodContentByID(int userID)
        {
            string strSql = "SELECT TOP (1)MoodContent FROM User_Mood WHERE (FUser = @UserID) AND (IsLock = 0) ORDER BY ID DESC";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@UserID",SqlDbType.Int)};
            parameters[0].Value = userID;
            return (string)DBUtility.DbHelperSQL.GetSingle(strSql, parameters);
        }

    }
}
